﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		KeyBuilder.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 //   File:            KeyBuilder.cs
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion

using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace Dexter.Core.Keys {
	/// <summary>
	/// Implementation of <see cref="IKeyBuilder"/>
	/// </summary>
	public class KeyBuilder : IKeyBuilder {
		/// <summary>
		/// Creates an unique key for the specified string.
		/// </summary>
		/// <param name="value">The generate from.</param>
		///<exception cref="ArgumentNullException"> If <paramref name="value"/> is <c>null</c> or <c>empty</c>.</exception>
		public string Create ( string value ) {
			if(string.IsNullOrEmpty ( value ))
				throw new ArgumentException ( );

			return this.Create ( new[] {value} );
		}

		/// <summary>
		/// Creates an unique key for the specified strings.
		/// </summary>
		/// <param name="values">The generate from.</param>
		///<exception cref="ArgumentNullException"> If <paramref name="values"/> is <c>null</c>.</exception>
		public string Create ( IEnumerable <string> values ) {
			
			if(values == null)
				throw new ArgumentNullException ( );

			var longKey = string.Join ( "|" , values );

			var toHashBytes = Encoding.UTF8.GetBytes ( longKey );
			var hashAlgorithm = SHA1.Create ( );
			var hashed = hashAlgorithm.ComputeHash ( toHashBytes );
			var base64String = Convert.ToBase64String ( hashed );
			var key = base64String
				.Replace ( "/", "_" )
				.Replace ( "+", "-" )
				.Replace ( "=", "@" )
				;
			return key;
		}
	}
}
